import json

from flask import Blueprint, request
from flask_restful import Resource, Api
import DcUrls

configData = Blueprint('configData', __name__)
api = Api(configData)


class getDcData(Resource):
    """
         获取配置数据接口
    :return:
    """

    def get(self):
        # 列表搜索配置
        dcListSearch = setSearchData()
        # 列表按钮集合
        dcListButtons = getListButtons()

        # 列表请求信息
        dcListRequest = setDcRequest(DcUrls.Url + "/getListData", "post", "{}", True, setDcPage("0", "10"))
        # 列表配置信息
        dcList = setDcList(0, dcListRequest, dcListButtons, True, dcListSearch)
        # tab页集合
        dc_tabs = []
        dc_tab = setDcTab("首页", "house", dcList)
        dc_tabs.append(dc_tab)
        # 主页配置
        dc_main = setDcMain(0, dc_tabs)
        # 整体配置信息
        data = setDc(True, dc_main, False, {}, False, {})
        # 设置返回result
        result = setResult(data)
        return result

    def post(self):
        reqJson = json.loads(request.data)
        print(reqJson)
        # 列表搜索配置
        dcListSearch = setSearchData()
        # 列表按钮集合
        dcListButtons = getListButtons()

        # 列表请求信息
        dcListRequest = setDcRequest(DcUrls.Url + "/getListData", "post", "{}", True, setDcPage("0", "10"))
        # 列表配置信息
        dcList = setDcList(0, dcListRequest, dcListButtons, True, dcListSearch)
        # tab页集合
        dc_tabs = []
        dc_tab = setDcTab("首页", "house", dcList)
        dc_tabs.append(dc_tab)
        # 列表请求信息
        dcListRequest = setDcRequest(DcUrls.Url + "/getListDataNew", "post", "{}", True, setDcPage("0", "10"))
        # 列表配置信息
        dcList = setDcList(0, dcListRequest, dcListButtons, True, dcListSearch)
        dc_tab = setDcTab("新闻", "house", dcList)
        dc_tabs.append(dc_tab)
        # 列表请求信息
        dcListRequest = setDcRequest(DcUrls.Url + "/getListDataAddress", "post", "{}", True, setDcPage("0", "10"))
        # 列表配置信息
        dcList = setDcList(0, dcListRequest, dcListButtons, True, dcListSearch)
        dc_tab = setDcTab("通讯录", "house", dcList)
        dc_tabs.append(dc_tab)
        # 主页配置
        dc_main = setDcMain("0", dc_tabs)
        # 整体配置信息
        data = setDc(True, dc_main, False, {}, False, {})
        # 设置返回result
        result = setResult(data)
        return result


api.add_resource(getDcData, '/getDcData')


def getListButtons():
    dcListButtons = []
    # 内容中提交参数，可以有多个
    dcButtonCommit = []
    dcButtonRequest = setDcRequest(DcUrls.Url + "/edit", "post", "{}", False, {})
    # dcButtonCommit = setDcRequest("/editCommit", "post", "{}", False, {})
    dcListButton = setDcButton("详情", False, False, False, True, "打开详细", dcButtonRequest, None)
    dcListButtons.append(dcListButton)

    dcButtonCommit = []
    dcButtonRequest = setDcRequest(DcUrls.Url + "/add", "post", "{}", False, {})
    dcButtonCommit.append(setDcRequests("提交", DcUrls.Url + "/addCommit", "post", "{}", False, {}))
    dcButtonCommit.append(setDcRequests("提交一", DcUrls.Url + "/addCommit", "post", "{}", False, {}))
    dcButtonCommit.append(setDcRequests("提交二", DcUrls.Url + "/addCommit", "post", "{}", False, {}))
    dcListButton = setDcButton("新增", True, True, False, False, "新增", dcButtonRequest, dcButtonCommit)
    dcListButtons.append(dcListButton)

    dcButtonCommit = []
    dcButtonRequest = setDcRequest(DcUrls.Url + "/edit", "post", "{}", False, {})
    dcButtonCommit.append(setDcRequests("提交", DcUrls.Url + "/editCommit", "post", "{}", False, {}))
    dcListButton = setDcButton("编辑", True, True, False, False, "编辑", dcButtonRequest, dcButtonCommit)
    dcListButtons.append(dcListButton)
    dcButtonCommit = []
    dcButtonRequest = setDcRequest(DcUrls.Url + "/delete", "post", "{}", False, {})
    # dcButtonCommit = setDcRequest("http://www.toutiao.com", "post", "{}", False, {})
    dcListButton = setDcButton("删除", False, False, True, False, "是否删除", dcButtonRequest, None)
    dcListButtons.append(dcListButton)
    return dcListButtons


def setDcButton(title, isEdit, isCommit, isDiglog, isSingleClick, describe, dcButtonRequest, dcButtonCommit):
    """
    列表中按钮配置
    :param title: 按钮名称
    :param isEdit: 是否可编辑（新增，编辑）
    :param isCommit: 是否有提交内容（新增，编辑）
    :param isDiglog: 是否是弹窗（删除）
    :param isSingleClick: 配置列表点击事件
    :param describe: 文字表述，针对弹窗（删除：是否要删除xxx）
    :param dcButtonRequest:按钮点击获取数据的接口
    :param dcButtonCommit:按钮针对的提交数据接口
    :return:
    """
    dcButton = {}
    dcButton["Title"] = title
    dcButton["isEdit"] = isEdit
    dcButton["isCommit"] = isCommit
    dcButton["isDiglog"] = isDiglog
    dcButton["isSingleClick"] = isSingleClick
    dcButton["describe"] = describe
    dcButton["dc_button_request"] = dcButtonRequest
    dcButton["dc_button_commit"] = dcButtonCommit
    return dcButton


def setDcPage(pageNumber=0, pageSize=10):
    """
    分页数据
    :param pageSize: 页码
    :param pageNumber: 每页数量
    :return:
    """
    dcPage = {}
    dcPage["pageNumber"] = pageNumber
    dcPage["pageSize"] = pageSize
    return dcPage


def setDcRequests(title, url, method, requestData, isPage, dcPage):
    """
    网络请求参数配置-多层
    :param title 请求标题名称
    :param url: 网络地址--api接口
    :param method: 提交方法 post|get
    :param requestData: 请求参数
    :param isPage: 是否分页
    :param dcPage: 分页配置默认0页10行
    :return:
    """
    dcRequests = {}
    dcRequest = {}
    dcRequest["url"] = url
    dcRequest["method"] = method
    dcRequest["requestData"] = requestData
    dcRequest["isPage"] = isPage
    dcRequest["dc_page"] = dcPage
    dcRequests["title"] = title
    dcRequests["dc_request"] = dcRequest
    return dcRequests


def setDcRequest(url, method, requestData, isPage, dcPage):
    """
    网络请求参数配置
    :param url: 网络地址--api接口
    :param method: 提交方法 post|get
    :param requestData: 请求参数
    :param isPage: 是否分页
    :param dcPage: 分页配置默认0页10行
    :return:
    """
    dcRequest = {}
    dcRequest["url"] = url
    dcRequest["method"] = method
    dcRequest["requestData"] = requestData
    dcRequest["isPage"] = isPage
    dcRequest["dc_page"] = dcPage
    return dcRequest


def setDcList(Type, dcListRequest, dcListButton, isSearch, dcListSearch):
    """
    列表页面配置
    :param dcListSearch: 搜索选项
    :param isSearch: 是否有搜索功能
    :param Type: 类别样式
    :param dcListRequest: 列表网络请求参数
    :param dcListButton: 列表 按钮配置列表
    :return:
    """
    dcList = {}
    dcList["type"] = Type
    dcList["dc_list_request"] = dcListRequest
    dcList["dc_list_button"] = dcListButton
    dcList["isSearch"] = isSearch
    dcList["dc_list_search"] = dcListSearch
    return dcList


def setDcTab(tabName, imageName, dcList):
    """
    tab页面详细配置
    :param tabName: tab名称
    :param imageName: 图片
    :param dcList: 列表信息
    :return:
    """
    dcTab = {}
    dcTab["tabName"] = tabName
    dcTab["imageName"] = imageName
    dcTab["dc_list"] = dcList
    return dcTab


#
# def setDcTabs(dcTabs, dc_tab):
#     """
#     tab页面数组配置
#     :param dc_tab:
#     :return:
#     """
#     dcTabs.append(dc_tab)
#     return dcTabs


def setDcMain(Type=0, dc_tabs={}):
    """
    tab页面配置信息
    :param Type: tab页面显示的样式类别
    :param dc_tabs: tab配置数据
    :return: tab配置数据信息
    """
    dcMain = {}
    dcMain["type"] = Type
    dcMain["dc_tabs"] = dc_tabs
    return dcMain


def setDc(isMain=False, dc_main={}, isList=False, dc_list={}, isDetail=False, dc_detail={}):
    """
    主页数据配置
    :param isMain: 是否设置tab页面
    :param dc_main: tab页面数据
    :param isList: 是否设置列表页面
    :param dc_list: 列表页面数据
    :param isDetail: 是否设置详情页面
    :param dc_detail: 详情页面数据
    :return: 主页配置数据信息
    """
    dc = {}
    dc["isMain"] = isMain
    dc["dc_main"] = dc_main
    dc["isList"] = isList
    dc["dc_list"] = dc_list
    dc["isDetail"] = isDetail
    dc["dc_detail"] = dc_detail
    return dc


def setResult(data):
    """
    通用返回数据格式
    :param data: 详细数据
    :return: result
    """
    result = {"code": 200, "message": "获取配置成功", "data": data}
    return result


def setSearchData():
    data = []
    # options = []
    # dcContent = setViewConten("headImage", "头像", DcUrls.Url + "/static/images/title.png", "image", True, options)
    # data.append(dcContent)
    options = []
    dcContent = setViewConten("userName", "姓名", "", "string", False, options)
    data.append(dcContent)
    # options = [setDcOption("0", "男", ""), setDcOption("1", "女", "")]
    # dcContent = setViewConten("userSex", "性别", "", "radio", True, options)
    # data.append(dcContent)
    #
    # options = [setDcOption("chess", "象棋", ""), setDcOption("cards", "打牌", ""), setDcOption("swimming", "游泳", ""),
    #            setDcOption("football", "足球", "")]
    # dcContent = setViewConten("hobby", "爱好", "", "checkbox", True, options)
    # data.append(dcContent)
    #
    # options = [setDcOption("ceo", "首席执行官", ""), setDcOption("cfo", "首席财政官", ""), setDcOption("coo", "首席运营官", "")]
    # dcContent = setViewConten("post", "职业", "", "select", True, options)
    # data.append(dcContent)
    #
    # options = []
    # dcContent = setViewConten("birderDay", "生日", "", "date", True, options)
    # data.append(dcContent)
    #
    # options = []
    # dcContent = setViewConten("join", "加入时间", "", "datetime", True, options)
    # data.append(dcContent)
    #
    # options = []
    # dcContent = setViewConten("joinTime", "加入时间点", "", "time", True, options)
    # data.append(dcContent)
    return data


def setViewConten(key, text, value, Type, isOption, dcoption):
    dcConten = {}
    dcConten["key"] = key
    dcConten["text"] = text
    dcConten["value"] = value
    dcConten["type"] = Type
    dcConten["isOption"] = isOption
    dcConten["dc_option"] = dcoption
    return dcConten


def setDcOption(key, text, value):
    dcOption = {}
    dcOption["key"] = key
    dcOption["text"] = text
    dcOption["value"] = value
    return dcOption
